// Display a list of running programs
// By Ben 23:35 24/09/2016

#include <iostream>
#include <Windows.h>
#include <TlHelp32.h>

using namespace std;
using std::cout;
using std::endl;

int main(int argc, char *argv[]){
	wstring temp;
	PROCESSENTRY32 pe;
	HANDLE snapshot;
	BOOL pFound = false;
	string ProcessExe = "";

	snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);

	//Set the size for PROCESSENTRY32
	pe.dwSize = sizeof(pe);

	//Get first process
	pFound = Process32First(snapshot, &pe);

	//Output titles
	cout << "Process Lister v1.0" << endl;
	cout << "-----------------------" << endl;
	cout << "PID\t  \NAME" << endl;
	cout << "-----------------------" << endl;

	//Check if first process was found
	if (pFound){

		//Loop and get the next process
		while (Process32Next(snapshot, &pe) != FALSE){
			//Convert WCHAR to wstring
			temp = wstring(pe.szExeFile);
			//Convert wstring to a string
			ProcessExe = string(temp.begin(), temp.end());
			//Output the process ID and Exe filename.
			cout << pe.th32ProcessID << "\t  " << 
				ProcessExe.c_str() << endl;
		}
		//Close snapshot handle
		CloseHandle(snapshot);
	}

	return 0;
}